package com.xindaoapp.happypet.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.api.AddClient;
import com.xindaoapp.happypet.api.Address;
import com.xindaoapp.happypet.utils.CommonParameter.UserState;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.ProgressHUD;

/**
 * 我的订单
 * 
 * @author Administrator
 */
public class MyOrderActivity extends UMBaseActivity implements OnClickListener {
    private final String TAG = "MyOrderActivity";
    private FrameLayout loading;
    private Context mContext;
    private ImageButton back;

    private RelativeLayout fukuan;
    private RelativeLayout fahuo;
    private RelativeLayout shouhuo;
    private RelativeLayout wancheng;

    private TextView num1;
    private TextView num2;
    private TextView num3;
    private TextView num4;

    private int wait_pay = 0;
    private int wait_ship = 0;
    private int wait_goods = 0;
    private int finish = 0;

    private int flag = -1;

    /** 是否为第一次载入 */
    private boolean isFirstLoading = true;

    private MyOrderBroadCast boradCast;
    private ProgressHUD progressHUD;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myorder);
        mContext = MyOrderActivity.this;

        initView();
        if (!isFirstLoading)
            loading.setVisibility(View.GONE);

        getData();
        boradCast = new MyOrderBroadCast();
        registerReceiver(boradCast, new IntentFilter(Constants.ORDER_DETAIL));

    }

    private void initView() {
        loading = (FrameLayout) findViewById(R.id.loading);
        back = (ImageButton) findViewById(R.id.cancel);

        fukuan = (RelativeLayout) findViewById(R.id.my_order_1);
        fahuo = (RelativeLayout) findViewById(R.id.my_order_2);
        shouhuo = (RelativeLayout) findViewById(R.id.my_order_3);
        wancheng = (RelativeLayout) findViewById(R.id.my_order_4);

        num1 = (TextView) findViewById(R.id.tv_my_order_1);
        num2 = (TextView) findViewById(R.id.tv_my_order_2);
        num3 = (TextView) findViewById(R.id.tv_my_order_3);
        num4 = (TextView) findViewById(R.id.tv_my_order_4);

        fukuan.setOnClickListener(this);
        fahuo.setOnClickListener(this);
        shouhuo.setOnClickListener(this);
        wancheng.setOnClickListener(this);
        back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(MyOrderActivity.this, MyOrderListActivity.class);
        switch (v.getId()) {
            case R.id.my_order_1:
                flag = 1;
                intent.putExtra("flag", 1);
                startActivity(intent);
                break;
            case R.id.my_order_2:
                flag = 2;
                intent.putExtra("flag", 2);
                startActivity(intent);
                break;
            case R.id.my_order_3:
                flag = 3;
                intent.putExtra("flag", 3);
                startActivity(intent);
                break;
            case R.id.my_order_4:
                flag = 4;
                intent.putExtra("flag", 4);
                startActivity(intent);
                break;

            case R.id.cancel:
                finish();
                break;
            default:
                break;
        }
    }

    private void getData() {
        if (CommonUtil.checkNetState(mContext)) {
            RequestParams params = new RequestParams();
            params.put("userID", UserState.getUserUid());
            if (!isFirstLoading) {
                progressHUD = ProgressHUD.show(mContext, "正在加载...", true, true, null);
            }
            AddClient.get(Address.GET_ORDERNUMBER, params, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, String content) {
                    /*
                     * if (null != progressHUD && progressHUD.isShowing()) { progressHUD.dismiss();
                     * }
                     */

                    try {
                        JSONObject obj;
                        String data = null;
                        try {
                            obj = new JSONObject(content);
                            data = obj.getString("data");
                        } catch (JSONException e1) {
                            e1.printStackTrace();
                        }

                        if (null == data) {
                            Toast.makeText(mContext, "获取数据失败", 0).show();
                            return;
                        }

                        try {
                            obj = new JSONObject(data);
                            wait_pay = obj.getInt("wait_pay");
                            wait_ship = obj.getInt("wait_ship");
                            wait_goods = obj.getInt("wait_goods");
                            finish = obj.getInt("finish");
                        } catch (JSONException e1) {
                            e1.printStackTrace();
                        }

                        if (wait_pay > 0) {
                            num1.setVisibility(View.VISIBLE);
                            num1.setText(wait_pay + "");
                        }
                        if (wait_ship > 0) {
                            num2.setVisibility(View.VISIBLE);
                            num2.setText(wait_ship + "");
                        }
                        if (wait_goods > 0) {
                            num3.setVisibility(View.VISIBLE);
                            num3.setText(wait_goods + "");
                        }
                        if (finish > 0) {
                            num4.setVisibility(View.VISIBLE);
                            num4.setText(finish + "");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFinish() {
                    isFirstLoading = false;
                    if (null != progressHUD && progressHUD.isShowing()) {
                        progressHUD.dismiss();
                    }
                    if (loading != null && loading.getVisibility() == View.VISIBLE) {
                        loading.setVisibility(View.GONE);
                    }
                }

                @Override
                public void onFailure(Throwable error, String content) {
                    super.onFailure(error, content);
                }
            });
        } else {
            Toast.makeText(mContext, R.string.pLease_check_network, 0).show();
            return;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(boradCast);
    }

    private class MyOrderBroadCast extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            switch (flag) {
                case 1:
                    if (wait_pay - 1 > 0) {
                        num1.setText((wait_pay - 1) + "");
                    } else {
                        if (num1.getVisibility() == View.VISIBLE) {
                            num1.setVisibility(View.GONE);
                        }
                    }
                    break;
                case 2:
                    if (wait_ship - 1 > 0) {
                        num2.setText((wait_ship - 1) + "");
                    } else {
                        if (num2.getVisibility() == View.VISIBLE) {
                            num2.setVisibility(View.GONE);
                        }
                    }
                    break;
                case 3:
                    if (wait_goods - 1 > 0) {
                        num3.setText((wait_goods - 1) + "");
                    } else {
                        if (num3.getVisibility() == View.VISIBLE) {
                            num3.setVisibility(View.GONE);
                        }
                    }
                    break;
                default:
                    break;
            }
        }
    }
}
